package init;

import java.util.ArrayList;
import java.util.List;

import org.apache.xpath.operations.Mod;
import org.docx4j.docProps.variantTypes.Array;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import toubiao.dao.business.ContactDaoI;
import toubiao.dao.business.DepartmentDaoI;
import toubiao.dao.business.DesignerDaoI;
import toubiao.dao.business.PartADaoI;
import toubiao.domain.po.business.PoContact;
import toubiao.domain.po.business.PoDepartment;
import toubiao.domain.po.business.PoDesigner;
import toubiao.domain.po.business.PoPartA;

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = { "classpath:config/spring/spring.xml", "classpath:config/spring/spring-hibernate.xml"})  
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
public class InitDataBase {

	private Logger logger=LoggerFactory.getLogger(InitDataBase.class);
	
	@Autowired
	private DepartmentDaoI departmentDao;
	
	@Autowired
	private DesignerDaoI designerDao;
	
	@Autowired
	private PartADaoI partADao;
	
	@Autowired
	private ContactDaoI contactDao;
	
	@Test
	public void  initDepartmentAndDesigner() {
		PoDepartment department1=new PoDepartment();
		PoDepartment department2=new PoDepartment();
		department1.setName("一分");
		department2.setName("二分");
		
		departmentDao.save(department1);
		departmentDao.save(department2);
		
		List<PoDesigner> designerList=new ArrayList<>();
		for(long i=1;i<11;i++){
			PoDesigner designer=new PoDesigner();
			designer.setName("宁少林_"+i);
			designer.setPhone("186395500_"+i);
			designerList.add(designer);
		}
		
		
		
		for(long i=0;i<10;i++ ){
			PoDesigner designer=designerList.get((int)i);
			if(i%2==0){
				designer.setDepartment(department1);
			}else {
				designer.setDepartment(department2);
			}
			designerDao.save(designer);
		}
		
	}
	
	@Test
	public void initPartAAndContact(){
		
		PoPartA partA1=new PoPartA("河南联通", "河南", "", "", "联通", "省级");
		PoPartA partA2=new PoPartA("郑州联通", "河南", "郑州", "", "联通", "市级");
		PoPartA partA3=new PoPartA("河南移动", "河南", "", "", "移动", "省级");
		PoPartA partA4=new PoPartA("洛阳联通", "河南", "洛阳", "", "联通", "市级");
		PoPartA partA5=new PoPartA("云南联通", "云南", "", "", "联通", "省级");
		PoPartA partA6=new PoPartA("西藏移动", "河南", "", "", "移动", "省级");
		partADao.save(partA1);
		partADao.save(partA2);
		partADao.save(partA3);
		partADao.save(partA4);
		partADao.save(partA5);
		partADao.save(partA6);
		
		List<PoPartA> partAList=new ArrayList<>();
		partAList.add(partA1);
		partAList.add(partA2);
		partAList.add(partA3);
		partAList.add(partA4);
		partAList.add(partA5);
		
		for(int i=0;i<10;i++){
			PoContact contact=new PoContact();
			contact.setName("李杰辉_"+i);
			contact.setPhone("13937100_"+i);
			if(i%5==0){
				contact.setPartA(partA1);
			}
			if(i%5==1){
				contact.setPartA(partA2);
			}
			if(i%5==2){
				contact.setPartA(partA3);
			}
			if(i%5==3){
				contact.setPartA(partA4);
			}
			if(i%5==4){
				contact.setPartA(partA5);
			}
			
			contactDao.save(contact);
		}
		
	}
}
